package com.heima.behavior.service.impl;

import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApCollectionBehaviorService;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollectionBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionBehaviorService implements IApCollectionBehaviorService {

    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * APP 端-文章详情页-用户对文章的收藏或取消收藏行为
     * @param collectionBehaviorDTO
     * @return
     */
    @Override
    public ResponseResult handleCollection(CollectionBehaviorDTO collectionBehaviorDTO) {
        //1.校验参数
            //1.1校验登录状态
        Integer loginId = AppThreadLocalUtils.getUser();
        if(loginId == null || loginId.intValue() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
            //1.2获取用户的行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(loginId, null);
        if(apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "获取登录用户的行为实体失败");
        }
            //1.3处理幂等操作
        Query query = Query.query(Criteria.where("entry_id").is(apBehaviorEntry.getId()).and("article_id").is(collectionBehaviorDTO.getEntryId()));
        ApCollectionBehavior apCollectionBehavior = mongoTemplate.findOne(query, ApCollectionBehavior.class);
        if(apCollectionBehavior != null && collectionBehaviorDTO.getOperation().intValue() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "文章已收藏，请勿重复收藏");
        }
        //2.处理收藏或取消收藏行为
        if(collectionBehaviorDTO.getOperation().intValue() == 0){
            //2.1收藏
            apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setEntryId(apBehaviorEntry.getId());
            apCollectionBehavior.setArticleId(collectionBehaviorDTO.getEntryId());
            apCollectionBehavior.setType(collectionBehaviorDTO.getType());
            apCollectionBehavior.setCreatedTime(new Date());
            apCollectionBehavior.setPublishedTime(collectionBehaviorDTO.getPublishedTime());
            mongoTemplate.save(apCollectionBehavior);
        }else {
            //2.2取消收藏
            mongoTemplate.remove(query, ApCollectionBehavior.class);
        }
        //3.返回响应
        return ResponseResult.okResult();
    }
}
